import numpy as np
from loguru import logger

from CloudEngine.core.VTActor import VTActor


def fx_run(service, script: str):
    try:
        result = {}
        ac: VTActor = service.get_first_actor()
        if ac is not None:
            exec(
                script,
                {},
                {
                    "numpy": np,
                    "result": result,
                    "attributes": ac.attributes,
                },
            )
        else:
            exec(
                script,
                {},
                {
                    "numpy": np,
                    "result": result,
                },
            )
        return result
    except Exception as e:
        logger.error(f"Error running script: {e}")
        return {"error": str(e)}
